package org.southyin.securitysamples.service;

import org.southyin.securitysamples.commons.CommonsResp;
import org.southyin.securitysamples.mapper.CategoryMapper;
import org.southyin.securitysamples.model.Category;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional
@Service
public class CategoryService {
    @Autowired
    CategoryMapper categoryMapper;

    public CommonsResp insertCategory(Category category) {
        int i = categoryMapper.insertCategory(category);
        if (i != 0) {
            return CommonsResp.succeess();
        }
        return CommonsResp.failed();
    }

    public CommonsResp delCategoryById(long id) {
        int i = categoryMapper.delCategoryById(id);
        if (i != 0) {
            return CommonsResp.succeess();
        }
        return CommonsResp.failed();
    }

    public CommonsResp updateCategoryById(Category category) {
        int i = categoryMapper.updateCategoryById(category);
        if (i != 0) {
            return CommonsResp.succeess();
        }
        return CommonsResp.failed();
    }

    public List<Category> listCategories(String keyWords) {
        return categoryMapper.listCategories(keyWords);
    }

    public CommonsResp multiDel(long[] ids) {
        int i = categoryMapper.multiDel(ids);
        if (i != 0) {
            return CommonsResp.succeess();
        }
        return CommonsResp.failed();
    }

    public CommonsResp updateCategoryDelMark(Category category) {
        int i = categoryMapper.updateCategoryDelMark(category);
        if (i != 0) {
            categoryMapper.updateArticleStatus(category);
            return CommonsResp.succeess();
        }
        return CommonsResp.failed();
    }

    public List<Category> listEnabledCategory() {
        return categoryMapper.listEnabledCategory();
    }
}
